/*******************************************************************************
 * Simple C-K Editor
 * 
 * Copyright (C) 2009 Ingi BROWN - Centre de Gestion Scientifique - Ecole des Mines ParisTech
 * Licensed under the GNU General Public License Version 3.
 * 
 * Simple C-K Editor includes source code and libraries from the JUNG Project (Java Universal Network/Graph Framework) 
 * released under the BSD license. Copyright (c) 2003-2004,  Regents of the University of California and the JUNG Project.
 * 
 * See the License.txt file for more details.
 ******************************************************************************/

package edu.ensmp.ckEditor.gui;


import org.apache.commons.collections15.Factory;


import edu.ensmp.ckEditor.ckModel.CKModel.Concept;
import edu.ensmp.ckEditor.ckModel.CKModel.Knowledge;
import edu.ensmp.ckEditor.main.Launcher;
import edu.uci.ics.jung.graph.SparseGraph;

/**
 * This class is a personal implementation of the JUNG SparseGraph<V, E> We use
 * this class twice: once for using Concepts as Vertex for the Concept Pane and
 * once with Knowledge as Vertex for the Concept Pane
 */
@SuppressWarnings("serial")
public class Graph<V, E> extends SparseGraph<V, E> {

	private int nodeCount, edgeCount;
	private edu.ensmp.ckEditor.main.Logger logger;

	// These need to be public so that we can access them from the mouse
	// managers to create new vertices by left-clicking
	public Factory<Knowledge> knowledgeFactory;
	public Factory<Concept> conceptFactory;
	public Factory<Integer> edgeFactory;

	/**
	 * The class constructor
	 */
	public Graph() {

		nodeCount = 0;
		logger = Launcher.getLogger();

		// Knowledge factory - to create a new knowledge vertex
		knowledgeFactory = new Factory<Knowledge>() {
			public Knowledge create() {
				String knowledgeProposition = Launcher.getEditorGUI()
						.getPropositionTextField().getText();

				Knowledge newKnowledge = new Knowledge(nodeCount,
						knowledgeProposition);
				Launcher.getEditorGUI().getPropositionTextField().setText("");
				Launcher.getEditorGUI().getPropositionTextField().grabFocus();

				logger.addLine("New Knowledge: [K" + nodeCount + "-"
						+ knowledgeProposition + "]");

				nodeCount++;
				return newKnowledge;
			}
		};

		// Concept factory - to create a new concept
		conceptFactory = new Factory<Concept>() {
			public Concept create() {
				String conceptProposition = Launcher.getEditorGUI()
						.getPropositionTextField().getText();
				Concept newConcept = new Concept(nodeCount, conceptProposition);
				Launcher.getEditorGUI().getPropositionTextField().setText("");
				Launcher.getEditorGUI().getPropositionTextField().grabFocus();
				logger.addLine("New Concept: [C" + nodeCount + "-"
						+ conceptProposition + "]");
				nodeCount++;
				return newConcept;
			}
		};

		// Edge factory - to create a new edge
		edgeFactory = new Factory<Integer>() {
			public Integer create() {
				return edgeCount++;
			}
		};
	}
}
